﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SMTC.BO.Employee;
using SMTC.Model.TestCore;
using SMTC.WebCommon.Common;
using System.Web.UI.HtmlControls;

public partial class UserPower_Employee : BasePage
{
    private EmployeeQueryServices _employeequeryBo = new EmployeeQueryServices();
    private EmployeeProccessServices _servers = new EmployeeProccessServices();
    protected override void OnPreRender(EventArgs e)
    {
        if (Request["action"] != null && Request["action"].ToString() == "select")
        {
            HtmlGenericControl tagBase = new HtmlGenericControl("base");
            tagBase.Attributes.Add("target", "_self");
            this.Page.Header.Controls.Add(tagBase);
        }
        base.OnPreRender(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["action"] != null && Request["action"].ToString() == "select")
            {
                SelectMemberGridviewAdduserbind();
            }
            else
            {
                GridviewAdduserbind();
            }
           
        }
        
        
    }

    private void SelectMemberGridviewAdduserbind()
    {
        ViewState["RoleID"] = Request["RoleID"].ToString();
        this.EmployeeGridview.Columns[0].Visible = true;
        this.EmployeeGridview.Columns[11].Visible =false;
        this.EmployeeGridview.Columns[12].Visible = false;
        for (int i = 4; i < 10;i++ )
        {
            this.EmployeeGridview.Columns[i].Visible = false;
        }
        btnAdd.Visible = false;
        btnOK.Visible = true;
        btnVendorContactCancel.Visible = true;
        title.Text = Resources.Resource.ChooseMember;
        //过滤已有成员
        List<EmpInfo> list = _employeequeryBo.Employee_For_Role_GridViewBind(ViewState["RoleID"].ToString());
        EmployeeGridview.DataSource = list;
        EmployeeGridview.DataBind();
    
    }
    private void GridviewAdduserbind()
    {

        List<EmpInfo> list = _employeequeryBo.SelectAllEmployeeGridViewBind();
        EmployeeGridview.DataSource = list;
        EmployeeGridview.DataBind();
        EmployeeGridview.Columns[0].Visible =false;
        for (int i = 0; i < EmployeeGridview.Rows.Count; i++)//当导入的员工编号时，改变背景色
        {
            for (int j = 0; j < EmployeeGridview.Rows.Count; j++)
            {
                if (((Label)EmployeeGridview.Rows[i].Cells[1].Controls[1]).Text == ((Label)EmployeeGridview.Rows[j].Cells[1].Controls[1]).Text && i != j)
                {
                    EmployeeGridview.Rows[i].Cells[1].BackColor = System.Drawing.Color.Aquamarine;
                    EmployeeGridview.Rows[j].Cells[1].BackColor = System.Drawing.Color.Aquamarine;
                }
            }
        
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.EmployeeGridview.DataSource = _employeequeryBo.ForSelectAllEmployeeGridViewBind(txtEmpNumber.Text.Trim(),txtEmpName.Text.Trim());
        this.EmployeeGridview.DataBind();
        EmployeeGridview.Columns[0].Visible = false;
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
            if (_employeequeryBo.ExistEmployee(txtEmployeNbr.Text.Trim()) == true)
            {
                ShowMessage(Resources.Resource.EmployeeMag1);//"员工编号重复！请重新添加！"
            }
            else
            {
                if (_servers.InsertEmployee(GetEmployee()))
                {
                    ShowMessage(Resources.Resource.EmployeeInfoAddSuccess);//"添加员工成功！"
                }
                else
                {
                    ShowMessage(Resources.Resource.EmployeeInfoAddFailed);//"添加员工失败！"
                }
              
            }
    }
    protected void btnUpd_Click(object sender, EventArgs e)
    {
            if (_servers.UpdateEmployee(GetEmployee()))
            {
                ShowMessage(Resources.Resource.EmployeeInfoUpdSuccess);//"维护员工成功！"
            }
            else
            {
                ShowMessage(Resources.Resource.EmployeeInfoUpdFailed);//"维护员工失败！"
            }
            GridviewAdduserbind();
          
    }
    protected void EmployeeGridview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            try
            {
                if (_servers.DeleteEmployee( e.CommandArgument.ToString()))
                {
                    ShowMessage(Resources.Resource.EmployeeInfoDelSuccess);//"删除员工成功！"
                }
                else
                {
                    ShowMessage(Resources.Resource.EmployeeInfoDelFailed );//"删除员工失败！"
                }
                GridviewAdduserbind();

            }
            catch (Exception ex)
            {

            }
        }

    }
    private EmpInfo GetEmployee()
    {
        EmpInfo employee = new EmpInfo();
        employee.UserID = hidUserID.Value;
        employee.UserName = txtUserName.Text.Trim();
        employee.UserPsw = txtPsw.Text.Trim();
        employee.EmpName = txtEmployeName.Text.Trim();
        employee.EmpNbr = txtEmployeNbr.Text.Trim();
        employee.MTel=txtEmployeMTel.Text.Trim();
        employee.EmpDep = txtEmployeDepart.Text.Trim();
        employee.Position = txtEmployePosition.Text.Trim();
        employee.ExtNum = txtEmployeExt.Text.Trim();
        employee.EMail = txtEmployeEmail.Text.Trim();
        employee.Fax = txtEmpFax.Text.Trim();
        employee.Remark = txtEmpRemark.Text.Trim();
        return employee;
    }
    protected void EmployeeGridview_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        if (e.NewPageIndex >= 0 && e.NewPageIndex < EmployeeGridview.PageCount)
            EmployeeGridview.PageIndex = e.NewPageIndex;
       
    }
    protected string pass(string pass)
    {
        string result = "";
        for (int i = 1; i <=pass.Length; i++)
        {
            result += "●";
        }
        return result;
    }
    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        Server.Transfer("UpLoadEmployee.aspx");
    }

}